{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b99e77d6",
   "metadata": {},
   "source": [
    "# 本周目标\n",
    "> 1. 函数的定义\n",
    "> 2. 如何使用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd03eb04",
   "metadata": {},
   "source": [
    "## 1.函数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "af06607a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello world\"+username+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "636c5673",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world华华~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"华华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "45a6ee13",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    print(2*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "06cd2b87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    }
   ],
   "source": [
    "f(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bc0e4f67",
   "metadata": {},
   "outputs": [],
   "source": [
    "def nfu_news_url(类别,page):\n",
    "    for page in range(page):\n",
    "          print(\"https://www.nfu.edu.cn/ztb/index.htm\" +str(page) +\".htm\")\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b6627f44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.nfu.edu.cn/ztb/index.htm0.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm1.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm2.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm3.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm4.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm5.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm6.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm7.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm8.htm\n",
      "https://www.nfu.edu.cn/ztb/index.htm9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"tzgg\",10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a5fd65b",
   "metadata": {},
   "source": [
    "## 2.如何使用函数 (传递实参）\n",
    "> 1. positional argument\n",
    "> 2. keyword argument(关键字参数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c28f6d8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "15f068ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a420efca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9b90071d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8162f62c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "42b69be7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "f(3,4,5,) #这个应该是位置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9bda379b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "6\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "f(y=6,x=0,z=9) #这个应该是关键字参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bc19e3a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9b4ddd15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ff2a475e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "f(3,10,11)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf4adc47",
   "metadata": {},
   "source": [
    "> 3.默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a109e91e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "54ba9337",
   "metadata": {},
   "source": [
    "# 3. 函数的返回值\n",
    "> 1. 几乎所有编程语言，函数是要求必须有返回值\n",
    "> 2. python作为高级语言，可以不写返回值，但是一般情况下，也是要写的，不写的话，则none\n",
    "> 3. 函数的返回值：return\n",
    ">> 3.1  return 可以返回过程运算的结果\n",
    ">> 3.2  return 可以返回过程自定义结果（由程序开发人员、设计人员定制的）\n",
    "## 4. 函数的优势\n",
    "* 1. 可重复使用\n",
    "* 2. 具有强大的可扩展性\n",
    "* 3. 具有可定制化性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2f7c7739",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y=x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "35ea50e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10bdf8b8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
